package com.dianrui.agent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.bean.Area;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FixAdapter extends BaseAdapter {

    Context context;
    List<Area>fixedList;
    AreaClickListener areaClickListener;

    public FixAdapter(Context context, List<Area> fixedList) {
        this.context = context;
        this.fixedList = fixedList;
    }

    public void refresh(List<Area>fixLists) {
        this.fixedList = fixLists;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return fixedList.size();
    }

    @Override
    public Object getItem(int position) {
        return fixedList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public interface AreaClickListener {
        void onAreaClick(String areaId);

    }

    public void setOnAreaClickListener(AreaClickListener areaClickListener) {
        this.areaClickListener = areaClickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fixed, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (fixedList.size()>0) {
            final Area area = fixedList.get(position);
            holder.showFixed.setText(area.area_name);
            holder.reMark.setText(area.remark);
            if ("1".equals(area.is_checked)) {
                holder.tickimg.setVisibility(View.VISIBLE);
            }else {
                holder.tickimg.setVisibility(View.GONE);
            }
            holder.areaLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    areaClickListener.onAreaClick(area.area_id);
                }
            });

        }
        return convertView;
    }
    class ViewHolder {
        @BindView(R.id.show_fixed)
        TextView showFixed;
        @BindView(R.id.remark)
        TextView reMark;
        @BindView(R.id.is_checks)
        ImageView tickimg;
        @BindView(R.id.area_layout)
        LinearLayout areaLayout;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
